Number of Islands
Question
You're given a 2D grid representing a map that consists of '1's (land) and '0's (water). An island is a group of adjacent land that's surrounded by water vertically or horizontally.
Return the number of islands on the map.
Input: grid = [[1, 0 ], [0, 1]]
Output: 2
There are two separate islands in the corner of the grid.
Input: grid = [[1, 1, 1, 1, 0], [0, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0]]
Output: 1
There is a single island in the top left corner of the grid.
Input: grid = [[1, 1, 1, 1, 1], [1, 0, 0, 0, 0], [1, 0, 1, 0, 0], [1, 0, 0, 0, 1], [1, 0, 1, 1, 1]]
Output: 3
There are three islands in this grid, one in the top-left edges, a second in the center, and a third in the bottom right corner of the grid.
Clarify the problem
What are some questions you'd ask an interviewer?
Understand the problem
Login or signup to save your code.
Uh oh... looks like you don't yet have access.
Not sure what this unlocks? Check out a free pattern section.